/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Transaction;

/**
 *
 * @author Wei Liu
 */
public class TransactionDAO extends BaseEntityDAO
{

    public TransactionDAO()
    {
        // initialization
    }

    /**
     * Insert transaction into database
     * @param transaction the new transaction entity to be inserted
     * @return newly created transaction number or a 0 on error
     */
    public int insertTransaction(Transaction transaction)
    {
        return save(transaction);
    }

    /**
     * Delete transaction that matches the supplied criteria
     * @param transaction the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteTransaction(Transaction transaction)
    {
        return delete(transaction);
    }

    /**
     * Update the transaction using data from the transaction Data Object
     * @param transaction the transaction Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateTransaction(Transaction transaction)
    {
        if (transaction.getOrderID() == 0 || transaction.getBankCardID() == 0)
        {
            return false;
        }

        if (update(transaction))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the transaction that matches the supplied criteria
     * @param transaction the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Transaction findTransaction(Transaction transaction)
    {
        String sql = null;

        if (transaction.getOrderID() != 0 && transaction.getBankCardID() != 0)
        {
            sql = buildQuerySQL(transaction);
        }
        else
        {
            return null;
        }

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(TransactionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Transaction) this.getObject(crs, transaction);
    }

    /**
     * Get all transactions that match the supplied criteria
     * @param transaction supplied criteria
     * @return a RowSet
     */
    public RowSet getTransactionsRS(Transaction transaction)
    {
        String sql = buildQuerySQL(transaction);
        return query(sql);
    }

    /**
     * Get all transactions that match the supplied criteria
     * @param transaction supplied criteria
     * @return a collection of Transaction Data Objects
     */
    public Collection<Transaction> getTransactionsDO(Transaction transaction)
    {
        String sql = buildQuerySQL(transaction);

        RowSet crs = query(sql);
        Collection<Transaction> transactions = new ArrayList<Transaction>();
        try
        {
            while (crs.next())
            {
                Transaction transactionDO = (Transaction) this.getObject(crs, transaction);
                transactions.add(transactionDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(TransactionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return transactions;
    }
}
